/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.swf.abc
{
	import sweezy.swf.IElement;
	import sweezy.swf.abc.opcode.UnknownOpecode;
	import sweezy.swf.constant.ElementType;
	import sweezy.swf.utils.CloneUtils;
	import sweezy.swf.visitor.IABCVisitor;

	public class Opcode implements IOpcode
	{

		public var position:uint;

		public var size:uint;

		private var _code:uint;

		private var _name:String;

		private var _typeName:String;

		private var _visitorName:String;

		public function Opcode(type:Class, unknownCode:int = -1)
		{
			_code = unknownCode > -1 ? unknownCode : Opcodes._codes[type];
			_name = Opcodes._names[_code];
			_typeName = Opcodes._typeNames[_code];
			_visitorName = Opcodes._visitorNames[_code];
		}

		public function accept(visitor:IABCVisitor):void
		{
			visitor[_visitorName](this);
		}

		public function clone():IElement
		{
			if (this is UnknownOpecode)
			{
				var src:UnknownOpecode = this as UnknownOpecode;
				var dst:UnknownOpecode = new UnknownOpecode(_code);
				dst.position = src.position;
				dst.size = src.size;

				return dst;
			}

			return CloneUtils.createClone(this);
		}

		public function get code():uint
		{
			return _code;
		}

		public function get elementName():String
		{
			return _typeName;
		}

		public function get elementType():uint
		{
			return ElementType.ABC_OPCODE;
		}

		public function get name():String
		{
			return _name;
		}
	}
}
